local ma__xingshi = fk.CreateSkill {
  name = "ma__xingshi",
}

Fk:loadTranslationTable{
  ["ma__xingshi"] = "兴师",
  [":ma__xingshi"] = "每轮开始时，你可以获得1枚你选择花色的“舰”标记（每种花色限1枚）:<br>"..
  "此“舰”所处角色死亡或与之花色相同的牌因弃置进入弃牌堆时，此“舰”移给顺时针方向的下一名角色（若移动给你则你摸两张牌并回复1点体力）。",

  ["#ma__xingshi"] = "兴师：获得一种花色的“舰”，同花色的牌被弃置后将顺时针方向移动",
  ["@ma__xingshi_ship"] = "舰",

  ["$ma__xingshi1"] = "布横江之铁索，徒自缚耳。",
  ["$ma__xingshi2"] = "艨艟击浪，可下千里江陵。",
}

ma__xingshi:addEffect(fk.RoundStart, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(ma__xingshi.name) and #player:getTableMark("xs_record") < 4
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local all_choices = {"log_spade", "log_heart", "log_club", "log_diamond"}
    local choices = table.filter(all_choices, function (s)
      return not table.contains( player:getTableMark("xs_record"), s)
    end)
    table.insert(all_choices, "Cancel")
    table.insert(choices, "Cancel")
    local choice = room:askToChoice(player, {
      choices = choices,
      skill_name = ma__xingshi.name,
      prompt = "#ma__xingshi",
      all_choices = all_choices,
    })
    if choice ~= "Cancel" then
      event:setCostData(self, {choice = choice})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local suit = event:getCostData(self).choice
    room:addTableMark(player, "@ma__xingshi_ship", suit)
    room:addTableMark(player, "xs_record", suit)
  end,
})

ma__xingshi:addEffect(fk.AfterCardsMove, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    local room = player.room
    if player:hasSkill(ma__xingshi.name) then
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile and move.moveReason == fk.ReasonDiscard then
          local costData = {}
          for _, info in ipairs(move.moveInfo) do
            local suit = Fk:getCardById(info.cardId):getSuitString()
            for _,p in ipairs(room.alive_players) do
              if table.contains(p:getTableMark("@ma__xingshi_ship"), suit)
                then table.insert(costData, suit)
              end
            end
          end
          if #costData > 0 then
            event:setCostData(self, costData)
            return true
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, suit in ipairs(event:getCostData(self)) do
      for _, p in ipairs(room.alive_players) do
        if table.contains(p:getTableMark("@ma__xingshi_ship"), suit) then
          if p:getNextAlive() ~= p then
            local new_mark = table.simpleClone(p:getTableMark("@ma__xingshi_ship"))
            table.removeOne(new_mark, suit)
            room:addTableMark(p, "@ma__xingshi_ship", #new_mark > 0 and new_mark or 0)
            local nextPlayer = p:getNextAlive()
            room:addPlayerMark(nextPlayer, "@ma__xingshi_ship", suit)
            if nextPlayer == player then
              player:drawcards(2, ma__xingshi.name)
              if player:isWounded() then
                room:recover { num = 1, skillName = ma__xingshi.name, who = player, recoverBy = player}
              end
            end
          end
        end
      end
    end
  end,
})

ma__xingshi:addLoseEffect(function (self, player, is_death)
  local room = player.room
  if table.every(room.alive_players, function (p)
    return not p:hasSkill(ma__xingshi.name, true)
  end) then
    for _, p in ipairs(room.alive_players) do
      room:setPlayerMark(p, "@ma__xingshi_ship", 0)
    end
  end
end)

return ma__xingshi